package com.by.service.impl;

import com.by.common.bean.PageBean;
import com.by.mapper.ProductManageMapper;
import com.by.pojo.dto.ProductPageQueryDTO;
import com.by.pojo.entity.Product;
import com.by.service.ProductManageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductManageServiceImpl implements ProductManageService {

    @Autowired
    private ProductManageMapper productManageMapper;


    @Override
    public Integer addProduct(Product product) {
        return productManageMapper.save(product);
    }

    @Override
    public Integer updateProduct(Product product) {
        return productManageMapper.update(product);
    }

    @Override
    public Integer deleteProduct(Integer id) {
        return productManageMapper.delete(id);
    }

    @Override
    public Product selectById(Integer id) {
        return productManageMapper.selectById(id);
    }


    @Override
    public PageBean query(ProductPageQueryDTO productPageQueryDTO) {
        int page = 1;
        int pageSize = 10;
        if(productPageQueryDTO.getPage() != null) page = productPageQueryDTO.getPage();
        if(productPageQueryDTO.getPageSize() != null) pageSize = productPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        List<Product> list = productManageMapper.query(productPageQueryDTO);
        PageInfo info = new PageInfo(list);
        return new PageBean<>(page, pageSize, info.getTotal(), list);
    }
}
